package com.coco.goods.controller;

import com.coco.common.jsrinterface.InsertGroup;
import com.coco.common.result.ResultData;
import com.coco.goods.pojo.TbCategory;
import com.coco.goods.service.CategoryService;
import com.coco.goods.vo.CategorySearchVo;
import com.coco.goods.vo.CategoryTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Package: com.coco.goods.controller
 * @Author: 张dy
 * @Date: Created in 2021/7/15 上午 10:45
 */

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 三级联动
     * @param id
     * @return
     */
    @RequestMapping("/linkage/{id}")
    public List<TbCategory> linkage(@PathVariable("id") Long id) {
        return categoryService.linkage(id);
    }

    /**
     * 初版 有空属性
     * @author HanSama
     * @date 2021/7/15 20:52
     * @return java.util.List<com.coco.goods.vo.CategoryTreeVo>
     */
    @RequestMapping("/getCategoryTree")
    public List<CategoryTreeVo> getCategoryTree(){
        return categoryService.getCategoryTree();
    }

    /**
     * 优化 无空属性
     * @author HanSama
     * @date 2021/7/15 20:52
     * @return java.util.List<java.util.Map>
     */
    @RequestMapping("/findTree")
    public List<Map> findTree(){
        return categoryService.findTree();
    }

    /**
     * 返回全部列表
     * @return
     */
    @GetMapping("/findAll")
    public List<TbCategory> findAll(){
        List<TbCategory> categoryList = categoryService.findAll();
        return categoryList;
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    public TbCategory findOne(Long id){
        return categoryService.findOne(id);
    }


    /**
     * 添加商品分类
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    public void addCategory(@Validated({InsertGroup.class}) @RequestBody TbCategory category){
        try {
            categoryService.addCategory(category);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改商品分类
     * @param category
     * @return
     */
    @PutMapping("/update")
    public void update(@RequestBody TbCategory category){
        try {
            categoryService.update(category);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询
     *
     * @param
     * @return
     */
    @PostMapping("/search")
    public ResultData search(@RequestBody CategorySearchVo categorySearchVo ){
        return categoryService.findAllSearch(categorySearchVo);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     */
    @PutMapping("/updStatus/{id}/{status}")
    public void updStatus(@PathVariable Long id, @PathVariable Integer status){
        try {
            categoryService.updStatus(id,status);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 列表树形
     * @author HanSama
     * @date 2021/7/16 9:11
     * @param searchVo
     * @return java.util.List<com.coco.goods.pojo.TbCategory>
     */
    @RequestMapping("/searchCategory")
    public List<Map> searchCategory(@RequestBody CategorySearchVo searchVo) {
        return categoryService.searchCategory(searchVo);
    }

    /**
     * 逻辑删除
     *
     * @param id
     */
    @DeleteMapping("/deleteById/{id}")
    public void  deleteById(@PathVariable("id") Long id){
        categoryService.deleteById(id);
    }
}
